//
//  CZVisitorView.swift
//  weibo24
//
//  Created by 李文峰 on 16/2/26.
//  Copyright © 2016年 fengten. All rights reserved.
//

import UIKit
import SnapKit

//代理方法实现按钮点击事件

//代理协议
protocol CZVisitorViewDelegate: NSObjectProtocol{
    
    //代理方法
    func didLogin()
    func didRegister()
}


class CZVisitorView: UIView {
    
    //代理属性
    weak var delegate: CZVisitorViewDelegate?

    let CZVisitorViewMargin: CGFloat = 10
    
//三部曲
//1.构造方法
    override init(frame: CGRect) {
        //调用父类方法
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//2.子控件的添加和约束setupUI
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        //添加控件
        
        
        
        addSubview(homeImageView)
        addSubview(iconImageView)
        addSubview(maskImageView)
        addSubview(loginButton)
        addSubview(registerButton)
        addSubview(noticeLabel)
        //控件约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        homeImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //遮罩视图
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
            
        }
        
        noticeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(CZVisitorViewMargin)
            make.width.equalTo(216)
            
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(noticeLabel.snp_left)
            make.top.equalTo(noticeLabel.snp_bottom).offset(CZVisitorViewMargin)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(noticeLabel.snp_right)
            make.top.equalTo(noticeLabel.snp_bottom).offset(CZVisitorViewMargin)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
    }
    
    //MARK:--动画转动
    func startAnimation(){
        
        let animtion = CABasicAnimation(keyPath: "transform.rotation")
        
        animtion.duration = 10
        
        animtion.toValue = 2 * M_2_PI
        
        animtion.repeatCount = MAXFLOAT
        
        // 切换界面的时候动画会被释放，指定为false之后切换界面动画就不会被释放
        animtion.removedOnCompletion = false
        
//        iconImageView.layer.addAnimation(<#T##anim: CAAnimation##CAAnimation#>, forKey: <#T##String?#>)
        iconImageView.layer.addAnimation(animtion, forKey: nil)
        
    }

    
    //MARK:--修改显示图片和文字
    func setupInfo(image:String,message:String,animtion:Bool){
        
        if animtion{
            
            startAnimation()
            
            iconImageView.hidden = false
        } else {
            iconImageView.hidden = true
        }
        
        noticeLabel.text = message
        homeImageView.image = UIImage(named: image)
    }
    
//3.懒加载
    //遮罩视图
    lazy var maskImageView: UIImageView = {
        
        let img = UIImageView()
        
        //背景图
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        //大小
//        img.sizeToFit()
        
        return img
        
        }()
    
    //背景旋转视图
    lazy var iconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
//        img.sizeToFit()
        
        return img
        }()
    
    //房子视图
    lazy var homeImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
        
        }()
    
    //文字提示
    lazy var noticeLabel: UILabel = {
        
        let lb = UILabel()
        
        lb.text = "关注一些人，回这里看看有什么惊喜"
        lb.font = UIFont.systemFontOfSize(13)
        lb.textColor = UIColor.blackColor()
        lb.numberOfLines = 0
        lb.textAlignment = NSTextAlignment.Center
        lb.sizeToFit()
        
        return lb
        }()

    //登录按钮
    lazy var loginButton: UIButton = {
        
        let bt = UIButton(type: UIButtonType.Custom)
        
        bt.setTitle("登录", forState: UIControlState.Normal)
        
        bt.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        bt.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //添加点击事件
        bt.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return bt
        
        }()
    
    //注册按钮
    lazy var registerButton: UIButton = {
        
        let bt = UIButton(type: UIButtonType.Custom)
        
        
        bt.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        bt.setTitle("注册", forState: UIControlState.Normal)
        bt.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        bt.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        

        
        return bt
        
        }()
    
    //按钮的点击事件
    func login(){
        
        delegate?.didLogin()
    }
    func register(){
       
        delegate?.didRegister()
    }
    
}
